/**
 * 玩家类
 * 控制玩家角色的移动和攻击
 */
class Player {
    /**
     * @param {number} x - 初始X坐标
     * @param {number} y - 初始Y坐标
     * @param {number} radius - 玩家半径
     */
    constructor(x, y, radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.path = [];
        this.currentPathIndex = 0;
        this.speed = 1;
        this.gridSize = 20;
        this.alignToGrid();
    }

    /**
     * 将玩家位置对齐到格子中心
     */
    alignToGrid() {
        this.x = Math.floor(this.x / this.gridSize) * this.gridSize + this.gridSize / 2;
        this.y = Math.floor(this.y / this.gridSize) * this.gridSize + this.gridSize / 2;
    }

    /**
     * 更新玩家状态
     */
    update() {
        if (this.path.length > 0 && this.currentPathIndex < this.path.length) {
            const targetPos = this.path[this.currentPathIndex];
            const targetX = targetPos.x * this.gridSize + this.gridSize / 2;
            const targetY = targetPos.y * this.gridSize + this.gridSize / 2;

            const dx = targetX - this.x;
            const dy = targetY - this.y;
            const distance = Math.sqrt(dx * dx + dy * dy);

            if (distance <= this.speed / 2) {
                this.x = targetX;
                this.y = targetY;
                this.currentPathIndex++;
            } else {
                const moveX = (dx / distance) * this.speed;
                const moveY = (dy / distance) * this.speed;
                
                this.x += moveX;
                this.y += moveY;
            }
        }
    }

    /**
     * 绘制玩家和相关元素
     */
    draw(ctx) {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
        ctx.fillStyle = '#0000FF';
        ctx.fill();
        ctx.closePath();

        if (this.path.length > 0) {
            ctx.beginPath();
            ctx.moveTo(this.x, this.y);
            
            for (let i = this.currentPathIndex; i < this.path.length; i++) {
                const pathX = this.path[i].x * this.gridSize + this.gridSize / 2;
                const pathY = this.path[i].y * this.gridSize + this.gridSize / 2;
                ctx.lineTo(pathX, pathY);
            }
            
            ctx.strokeStyle = 'rgba(0, 255, 0, 0.3)';
            ctx.stroke();
            
            if (this.currentPathIndex < this.path.length) {
                const currentTarget = this.path[this.currentPathIndex];
                const targetX = currentTarget.x * this.gridSize + this.gridSize / 2;
                const targetY = currentTarget.y * this.gridSize + this.gridSize / 2;
                
                ctx.beginPath();
                ctx.arc(targetX, targetY, 3, 0, Math.PI * 2);
                ctx.fillStyle = '#00FF00';
                ctx.fill();
            }
        }
    }

    // 添加新方法来设置路径
    setPath(newPath) {
        if (!newPath || newPath.length === 0) {
            this.path = [];
            this.currentPathIndex = 0;
            return;
        }
        
        const currentTarget = this.path[this.path.length - 1];
        const newTarget = newPath[newPath.length - 1];
        
        if (!currentTarget || !newTarget || 
            currentTarget.x !== newTarget.x || 
            currentTarget.y !== newTarget.y) {
            this.path = newPath;
            this.currentPathIndex = 0;
        }
    }
} 